<?php

namespace app\admin\model;

use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\exception\ValidateException;
use think\Model;

class Role extends Model
{



    protected $autoWriteTimestamp='datetime';
    public function getStatusAttr($value)
    {
        $Status = [1=>'显示',0=>'隐藏'];
        return $Status[$value];
    }

    public function getStoreInfo(){

    return    $this->hasOne(Store::class,'id','store_id')->bind(['store_name'=>'name']);
    }

    /**
     * @throws ModelNotFoundException
     * @throws DataNotFoundException
     * @throws DbException
     */
    public  static function getList(){

        $where=[];
        $store_json=session('store_id');
        $where[]=['id','in',$store_json];
        
        return  self::with(['getStoreInfo'])->where($where)->page(input('page',1))->limit(input('limit',20))->select();

    }
    public static function insertRole($add){
        try {
            $result=self::create($add);
            if($result){
                $data = array(
                    'code' => 200,
                    'msg' => '成功!',
                    'url'=>url('/Department/roleList')->build()
                );
                return $data;
            }else{
                $data = array(
                    'code' => 40002,
                    'msg' => '失败!',
                );
                return $data;
            }
            // dump($result);die;

        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return $e->getError();
        }

    }

    public static function modifyRole($add)
    {
        try {
            $result = self::where(['id' => input('id')])->save($add);

            if (!empty($result)) {
                $data = array(
                    'code' => 200,
                    'msg' => '成功!',
                    'url' => url('/Department/roleList')->build()
                );

                return $data;
            } else {
                $data = array(
                    'code' => 40002,
                    'msg' => '成功!',
                );
                return $data;
            }

        } catch (ValidateException $e) {
            // 验证失败 输出错误信息

            return $e->getError();
        }
    }

}